package cn.stylefeng.guns.modular.api.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Course;
import cn.stylefeng.guns.modular.rms.model.params.CompanyParam;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.rms.model.params.CourseApplyParam;
import cn.stylefeng.guns.modular.rms.model.params.CourseParam;
import cn.stylefeng.guns.modular.rms.model.result.CompanyPositionResult;
import cn.stylefeng.guns.modular.rms.model.result.CompanyResult;
import cn.stylefeng.guns.modular.rms.model.result.CourseApplyResult;
import cn.stylefeng.guns.modular.rms.model.result.CourseResult;
import cn.stylefeng.guns.modular.rms.service.CompanyPositionService;
import cn.stylefeng.guns.modular.rms.service.CompanyService;
import cn.stylefeng.guns.modular.rms.service.CourseApplyService;
import cn.stylefeng.guns.modular.rms.service.CourseService;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 课程管理相关接口
 */
@Controller
@RequestMapping("/api/course")
@Api(tags = "课程管理相关接口")
public class ApiCourseController extends BaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseApplyService courseApplyService;

    /**
     * 查询课程列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCourseList", method = RequestMethod.GET)
    @ApiOperation(value = "查询课程列表", notes = "查询课程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getCourseList(CourseParam param) {
        LayuiPageInfo pageInfo = courseService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }

    /**
     * 查询课程信息
     */
    @ResponseBody
    @RequestMapping(value = "/getCourseDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询课程信息", notes = "查询课程信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public ResponseData getCompanyDetail(Long id) {
        if (ObjectUtil.isEmpty(id)) {
            return ResponseData.error("id为空");
        }
        CourseParam param1 = new CourseParam();
        param1.setId(id);
        CourseResult result = courseService.findBySpec(param1);
        try {
            //查看是否已报名
            CourseApplyParam param = new CourseApplyParam();
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
            param.setCourseId(id);
            CourseApplyResult ar = courseApplyService.findBySpec(param);
            if (ar != null) {
                result.setIfApply(true);
            } else {
                result.setIfApply(false);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return ResponseData.success(result);
    }

    /**
     * 课程报名
     */
    @ResponseBody
    @RequestMapping(value = "/applyCourse", method = RequestMethod.POST)
    @ApiOperation(value = "课程报名", notes = "课程报名")
    public ResponseData applyCourse(@RequestBody CourseApplyParam param) {
        //取得当前Token登录者会员的ID
        try {
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
            courseApplyService.add(param);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }


    /**
     * 查询我的课程列表
     */
    @ResponseBody
    @RequestMapping(value = "/getApplyCourseList", method = RequestMethod.GET)
    @ApiOperation(value = "查询我的课程列表", notes = "查询我的课程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getApplyCourseList(CourseApplyParam param) {
        try {
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
            LayuiPageInfo pageInfo = courseApplyService.findPageBySpec(param);
            return ResponseData.success(pageInfo);
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }

}



